home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Floppyshop 2
/
Floppyshop - 2.zip
/
Floppyshop - 2.iso
/
diskmags
/
0022-3.564
/
dmg-3557
/
news.txt
/
asmtut.asc
< prev
next >
Wrap
Text File
|
1989-04-05
|
10KB
|
278 lines
#######################
# #
# Assembly Tutorial #
# ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ #
# by Duncan and #
# Robert Annett #
# #
#######################
This article should have appeared in Stupendous several months ago but as
it hasn't I will let STEN readers have a read of it.
I hope you like it. If you do then perhaps I could do some regular
assembler stuff in STEN. This was mostly written by Duncan Annett with
this introduction and some bits and pieces by me, Robert Annett.
So here goes....
One of the best ways to learn a language is to follow an example program,
below is one such example. The program asks the user (i.e. you!) to input
the name of a Degas picture file and then loads it onto the screen.
First of all a quick note for when you assemble and run the program:-
- assemble the program to disk giving it a .TOS extension.
- run the program in low resolution to load a .PI1
picture.
- the picture must be in the same directory.
(The program listing may also be found in the PROGRAMS folder)
****Picture Loader****
--------------------------------------------------------------------------
startup move.l 4(sp),a3
move.l $c(a3),d0 ;length of text
add.l $14(a3),d0 ;length of data constants
add.l $1c(a3),d0 ;length of data storage
add.l #500,d0 ;additional memory
add.l #$100,d0 ;length of basepage
move.l a7,oldstack
lea endstack(pc),a7
move.l d0,-(a7)
move.l a3,-(a7)
clr -(a7)
move #$4a,-(a7)
trap #1
lea 12(sp),sp
When a program is run, it grabs all free memory for itself. Normally this
does not matter, however if your program uses any functions that manage
the memory, for example allocating a memory block or disk functions, then
some memory has to be given back to the system. It is good pratice to do
this in whatever program you write. This is what the above section does.
It calculates the length of the program in D0, saves the address of A7
(also called the stack pointer [SP]) at the location oldstack, places the
position of A7 in our program (at location endstack) and then performs the
neccessary TRAP call.
Trap calls are routines stored in the Atari's ROM and are very useful.For
instance, if a computers' hardware changes like the STFM and STE then
programs should still work as the TRAP call will handle the hardware
changes. If programmers get fancy and write programs to access the
hardware directly, then it will fail to work on the new hardware, this is
what happened with the STE.
Another fact about the TRAP is that they destroy the contents of registers
D0-D2 and A0-A2, so if valuable data is stored in them then save those
registers. Also remember to correct the stack.This is the line lea
12(sp),sp this adds 12 to the stack pointer (A7) and stops it from writing
over our program.
--------------------------------------------------------------------------
setstuff move.l #0,-(sp) ;0 - low 1-med 2-high
move.l #$78000,-(sp)
move.l #$78000,-(sp)
move #5,-(sp)
trap #14
add.l #14,sp
This section sets up the screen. The first line changes to low resolution
(this only works for TOS programs, GEM will not adjust to the correct
resoltion, this is why the program is assembled to .TOS). The next two
lines are the addresses of the LOGICAL and PHYSICAL screens. I will not go
explain these in this article but just assume they are both the same
(their address must be on a 256 byte boundary (STFMs' only, STEs' can be
on any even boundary))
--------------------------------------------------------------------------
printline move.l #text,-(sp)
move #9,-(sp)
trap #1
add.l #6,sp
This section prints a message on the screen.This message is stored at
location text (the # sign means 'the address of', if there was no # and
just 'text' it means 'the data stored in the address of'). This message
can be changed to whatever you want and can incoporate VT52 control codes
(they would be placed outside of the quotes, seperated by commas, and
before the final 0).
The text must always have a null character at the end, this is simply a 0.
--------------------------------------------------------------------------
input move.l #inpstart,-(sp)
move #$a,-(sp)
trap #1
add.l #6,sp
lea buff(pc),a1
add.w d0,a1
move.b #0,(a1)
This section asks the user for an input. The first line places the address
of a buffer to the stack. The first two bytes are reserved, the first
containing the maximum length of the string to be inputted and the second
contains the actual length of string that was input (also contained in
D0).
The last three lines place a null at the end of the input, this is for
later use. Basically, A1 points to the start of the input data (missing
out those first two bytes), then the length of the string is added to A1
and a zero is placed in this address.
--------------------------------------------------------------------------
openfile move #0,-(sp) ;0-read,2-write,3-read and write
move.l #buff,-(sp)
move #$3d,-(sp)
trap #1
add.l #8,sp
tst d0
bmi end
move d0,handle
This opens a file. The first line specifies which operations can be
carried, for this program we only want to read the file. Then the address
of the filename is placed on the stack, it has to be ended with a null
(this part was carried out in the previous section). This TRAP call
returns a value in D0, if it is negative then an error occured (e.g. could
not find the file) if this happens the program jumps to the end (bmi -
branch if minus). If not then the value is saved.
--------------------------------------------------------------------------
readpal move.l #paldat,-(sp)
move.l #34,-(sp)
move handle,-(sp)
move #$3f,-(sp)
trap #1
add.l #12,sp
setpal move.l #paldat+2,-(sp) ;add 2 to get to palette
move #6,-(sp)
trap #14
add.l #6,sp
This reads in the colour palette. Degas pictures have a 34 byte long
header, the first word (two bytes) contains the resolution mode and the
next 16 words contain the palette. #paldat is the address the colours will
be loaded to, #34 is the number of bytes to be read and handle is the
value of the file number.
Then the next TRAP call loads the colours into the hardware colour
registers, remembering that the palette data is two bytes in from the
start of the header.
--------------------------------------------------------------------------
readpic move.l physbase,-(sp)
move.l #32000,-(sp)
move handle,-(sp)
move #$3f,-(sp)
trap #1
add.l #12,sp
tst d0
bmi end
Much the same as before, physbase is the address of the screen (notice
that this places the data stored at physbase onto the stack). Also this
checks for errors D0 being negative if there are any. Remember the handle.
32000 is the length of the picture data.
--------------------------------------------------------------------------
closefile move handle,-(sp)
move #$3e,-(sp)
trap #1
add.l #4,sp
tst d0
beq wait
This closes the file. All that has to be passed is the file handle. It
then waits for a key to be pressed.
--------------------------------------------------------------------------
end move.l oldstack,a7
clr -(sp)
trap #1
This reloads the stack pointer and terminates the program. It needs no
stack correction because the program ends.
--------------------------------------------------------------------------
wait move #7,-(sp)
trap #1
add.l #2,sp
bra end
This waits for any key to be pressed and jumps to the end of the program.
--------------------------------------------------------------------------
even
newstack ds.l 100
endstack dc.l 0
oldstack dc.l 0
text dc.b "Name of picture? ",0
even
inpstart dc.b 12,0 ;input 12 chars MAX.
buff ds.b 15 ;buffer for user input
even
handle dc.w 0
physbase dc.l $78000
paldat ds.w 17
The data area. DC means Declare Constant and stores that value in memory.
DS means Declare Storage and reserves a block of memory.
~~~OOOO~~~
If you wish to contact Robert, then send your letters via myself at:
Michael McLaughlin
152 Hapland Road
Pollok
Glasgow G53 5PR
If you want a reply then send a S.A.E.
___ooOoo___